package com.vincent.kdp.Activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.vincent.baseapp.util.CommonUtil;
import com.vincent.kdp.PostUrl;
import com.vincent.kdp.R;
import com.vincent.kdp.http.RequestCallBack;
import com.vincent.kdp.js.GoodsDetailOperator;
import com.vincent.kdp.ui.bean.entity.CommodityDetail;
import com.vincent.kdp.ui.bean.entity.VipInfo;
import com.vincent.kdp.ui.bean.response.BaseOutput;
import com.vincent.kdp.ui.bean.response.CommodityDetailOutput;
import com.vincent.kdp.ui.commodity.repository.CommodityDetailRepository;
import com.vincent.kdp.ui.other.MainActivity;
import com.vincent.kdp.util.ActivityUtils;
import com.vincent.kdp.util.BaseParam;
import com.vincent.kdp.util.DialogUtils;
import com.vincent.kdp.view.LoadingWebChromeClient;

import java.util.Date;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by fuxingxing on 17/1/24.
 */

public class GoodsDetailActivity extends Activity {

    @Bind(R.id.back_img)
    ImageView mBackImg;
    @Bind(R.id.toolbar_center_tv)
    TextView mToolbarCenterTv;
    @Bind(R.id.webView)
    WebView webView;

    @Bind(R.id.go_to_cart)
    FrameLayout go_to_cart;


    private VipInfo vipInfo;

    private Activity thisActivity;

    private String barCode;
//    private String isScan;

    public static Activity currentActivity;

    public CommodityDetailRepository repository=new CommodityDetailRepository();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        thisActivity=this;
        if(currentActivity!=null){
            Log.i("code", "currentActivity!=null");
            currentActivity.finish();
        }else {
            Log.i("code", "currentActivity=null");
        }
        currentActivity=this;
        setContentView(R.layout.activity_vipinfo);
        ButterKnife.bind(this);
        getParam(getIntent());

        this.vipInfo = (VipInfo) getIntent().getSerializableExtra("VipInfo");
        Log.i("code", "onCreate");
    }

    @Override
    protected void onResume() {
        super.onResume();
        if("1".equals(BaseParam.getIsScan())){
            reqCommodityDetail();
        }else{
            mToolbarCenterTv.setText("商品详情");
            this.initializeWebView();
        }
        Log.i("code", "onResume");

    }

    @Override
    protected void onDestroy() {
        Log.i("code", "onDestroy");
        super.onDestroy();
    }

    @Override
    protected void onPause() {
        Log.i("code", "onPause");
        super.onPause();
    }

    @Override
    protected void onRestart() {
        Log.i("code", "onRestart");
        super.onRestart();
    }

    private void reqCommodityDetail(){
        repository.reqCommodityDetail(BaseParam.getCurrentBarCode(), BaseParam.getDeviceId(), new RequestCallBack<CommodityDetailOutput>() {
            @Override
            public void onSuccess(CommodityDetailOutput output) {
                    if("未查询到相关商品信息".equals(output.getMsg())){
                        Toast.makeText(GoodsDetailActivity.this, "未查询到相关商品信息", Toast.LENGTH_SHORT).show();
                        finish();
                        overridePendingTransition(com.vincent.baseapp.R.anim.fragment_left_in, com.vincent.baseapp.R.anim.fragment_right_out);
                    }else{
                        CommodityDetail detail=output.getData();
                        if("5".equals(detail.getStatus())){
                            Intent intent=new Intent(GoodsDetailActivity.this,CostDetailActivity.class);
                            intent.putExtra("barCode",detail.getBarCode());
                            startActivity(intent);
                            thisActivity.finish();
                        }else{
                            mToolbarCenterTv.setText("商品详情");
                            increaseLookTimes();
                            //initializeWebView();
                        }
                    }
            }

            @Override
            public void onFalse(String msg) {
                Toast.makeText(thisActivity, "未查询到相关商品信息", Toast.LENGTH_SHORT).show();
                finish();
                overridePendingTransition(com.vincent.baseapp.R.anim.fragment_left_in, com.vincent.baseapp.R.anim.fragment_right_out);
            }
        });
    }

    public String getBarCode() {
        return barCode;
    }

    private void getParam(Intent intent){
        String barCode=intent.getStringExtra("barCode");
        if(barCode==null){
            this.barCode="";
        }else{
            this.barCode=barCode;
            Log.i("code", "getParam--：" + barCode);
        }
//        String isScan=intent.getStringExtra("isScan");
//        if(isScan==null){
//            this.isScan="";
//        }else{
//            this.isScan=isScan;
//        }
    }

    @SuppressLint({ "NewApi", "SetJavaScriptEnabled" })
    private void initializeWebView(){
        webView.addJavascriptInterface(new GoodsDetailOperator(thisActivity,BaseParam.getCurrentBarCode()),
                "GoodsDetail");
        try {
            String url= PostUrl.SERVICE_URL+"/H5/goodDetail/goodDetail.html";
            WebSettings webSettings = webView.getSettings();
            webSettings.setJavaScriptEnabled(true);
            webSettings.setAllowFileAccess(true);
            webSettings.setAllowFileAccessFromFileURLs(true);
            webView.setWebChromeClient(new LoadingWebChromeClient(thisActivity));
            this.webView.loadUrl(url);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void increaseLookTimes(){
        repository.increaseLookTimes(BaseParam.getCurrentBarCode(), new RequestCallBack<BaseOutput>() {
            @Override
            public void onSuccess(BaseOutput object) {
                if(object.isSuccess()){
                    initializeWebView();
                }
            }

            @Override
            public void onFalse(String msg) {
                initializeWebView();
                Toast.makeText(thisActivity,msg,Toast.LENGTH_SHORT).show();
               // DialogUtils.getIsInstance().createDialog(msg,thisActivity);
                //thisActivity.finish();
            }
        });
    }


    @OnClick({R.id.back_img, R.id.go_to_cart})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_img:
                this. finish();
                overridePendingTransition(com.vincent.baseapp.R.anim.fragment_left_in, com.vincent.baseapp.R.anim.fragment_right_out);
                break;
            case R.id.go_to_cart:
                Intent it =new Intent(this, MainActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                Bundle bundle = new Bundle();
                bundle.putInt("index", 4);
                it.putExtras(bundle);
                startActivity(it);
                finish();

                break;
        }
    }


}
